package hong.jun.pet_home.controller.product;


import hong.jun.basic.util.AjaxResult;
import hong.jun.basic.util.PageList;
import hong.jun.pet_home.domain.product.ProductDetail;
import hong.jun.pet_home.query.ProductDetailQuery;
import hong.jun.pet_home.service.product.IProductDetailService;
import io.swagger.annotations.Api;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * restfull风格
 *
 * @Controller
 * @ResponseBody
 */

@RestController
@RequestMapping("/productdetail")
@Api(tags = "ProductController", description = "ProductController | 服务详情管理接口")
//@CrossOrigin//后台的跨域解决方案-spring4.2.5及以上版本支持
public class ProductDetailController {
    private static final Logger logger = LoggerFactory.getLogger(ProductDetailController.class);


    @Autowired
    private IProductDetailService productDetailService;

    @PutMapping
    public AjaxResult saveOrUpdate(@RequestBody ProductDetail productDetail) {
        //判断有无id决定新增还是修改
        try {
            if (productDetail.getId() != null) {
                productDetailService.update(productDetail);
            } else {
                productDetailService.save(productDetail);
            }
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(e.toString());
        }
    }

    /* 根据id 删除  */
    @DeleteMapping(value = "/{id}")
    public AjaxResult del(@PathVariable("id") Long id) {
        try {
            ProductDetail productDetail = productDetailService.getById(id);
            logger.info("删除" + productDetail.toString());//未查到对象为空会报空指针异常
            productDetailService.delete(id);
            return AjaxResult.me();
        } catch (Exception e) {
            logger.error(e.toString());
            return new AjaxResult(e.toString());
        }
    }

    //根据id 批量删除
    @PostMapping("/dels")
    public AjaxResult dels(@RequestBody Long[] ids) {
        try {
            productDetailService.deletes(ids);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(e.toString());
        }
    }

    /*根据Id查询*/
    @GetMapping(value = "/{id}")
    public ProductDetail getById(@PathVariable("id") Long id) {
        return productDetailService.getById(id);
    }

    @GetMapping
    public List<ProductDetail> getAll() {
        return productDetailService.getAll();
    }

    /*条件分页查询*/
    @PostMapping
    public PageList<ProductDetail> getPage(@RequestBody ProductDetailQuery query) {
        return productDetailService.queryPage(query);
    }
}
